package cn.net.codeplus.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.net.codeplus.entity.Project;
import cn.net.codeplus.service.IProjectService;

@Controller
public class IndexController {

	@RequestMapping({"/","index",""})
	public String index(ModelMap map) {
		return "redirect:/s";
	}
	
@Autowired private IProjectService projectService;
	
	/**
	 * 检索项目
	 * @param p
	 * @param q
	 * @param model
	 * @return
	 */
	@RequestMapping("/s")
	public String search(@RequestParam(defaultValue="1",required=false) Integer p, 
			@RequestParam(defaultValue="20",required=false) Integer s, 
			String q,
			String tag,
			Model model){
		EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
		wrapper.orderBy(true, "STAR", false);
		if(StringUtils.isNotBlank(tag)){
			wrapper.like("TAGS", tag);
			model.addAttribute("tag", tag);
		}else if(StringUtils.isNotBlank(q)){
			wrapper.like("PRJ_DESC", q).or().like("TAGS", q);
			model.addAttribute("q", q);
		}
		model.addAttribute("pageData",projectService.selectPage(new Page<>(p, s), wrapper));
		return "index";
	}
	
	/**
	 * 查询详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/d")
	public String detail(String id,Model model){
		model.addAttribute("project",projectService.selectById(id));
		return "detail";
	}

}
